#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''
import logging
from pycommon import Const
from pycommon import Pack as packet
from pycommon import Utils
import PlatformDb
import GameDb
import TimeCheck
import ClientBuild
from thirdpartygate import WeChatGate

def cmdLogin(client,pkt):
    account,passwd = packet.unpack("ss",pkt)
    logging.debug(u"account=%s,passwd=%s",account,passwd)
    ecode = Const.ERR_OK
    if not Utils.isValidAccountInternal(account):
        ecode = Const.ERR_INVALID_ACCOUNT
    elif not Utils.isValidPasswd(passwd):
        ecode = Const.ERR_INVALID_PASSWD 
    elif not TimeCheck.instance.canLogin(account):
        ecode = Const.ERR_WAIT_AND_RETRY 

    if ecode != Const.ERR_OK:
        client.sendPacket(ClientBuild.loginResult(ecode,u'',u''))
        return
    PlatformDb.instance.login(client,account,passwd)

def cmdRegister(client,pkt):
    account,passwd = packet.unpack("SS",pkt)
    regip = client.getIp()
    deviceid = u"00000000000"
    ecode = Const.ERR_OK
    if not Utils.isValidNormalAccount(account):
        ecode = Const.ERR_INVALID_ACCOUNT
    elif not Utils.isValidPasswd(passwd):
        ecode = Const.ERR_INVALID_PASSWD
    elif not Utils.isValidDeviceId(deviceid):
        ecode = Const.ERR_INVALID_DEVICEID
    elif not TimeCheck.instance.canDoRegisterIp(regip):
        ecode = Const.ERR_WAIT_AND_RETRY
    if ecode != Const.ERR_OK:
        logging.debug(u"cmdRegister() registerError,ecode = %d",ecode);
        client.sendPacket(ClientBuild.registerResult(ecode))
        return
    PlatformDb.instance.register(client,account,passwd)
    TimeCheck.instance.addRegisterIp(regip) 

def cmdAutoRegister(client,pkt):
    logging.debug(u"cmdAutoRegister")
    passwd = Utils.genAutoRegPasswd()
    regip = client.getIp()
    PlatformDb.instance.registerAuto(client, passwd, regip)
    pass


def cmdWeChatLogin(client,pkt):
    if False:
        a,b,c,code = packet.unpack("HHHs",pkt)
        logging.debug(u"WeChat login wxuid = %d,%d,%d wxtoken = %s",a,b,c,code)
        WeChatGate.instance.send(client.getId(),code)
    else:
        pid = client.getId()
        nickname = u'\u6211\u662f\u83dc\u9e1f'
        headimgurl = u'http://wx.qlogo.cn/mmopen/azOicib7sicR7mfakokXHTJVsSHBNG95Cdd13icsKrEt8t5w2Xicia21HcQMbBzYO4vp8Xicq1RIZR0ia2t3wYnXiciaIkaiaOx263Eic19m/0'
        sex = 2
        openid = u'opofJw4xa2wBQXYJdYX6ZXjFBac8'
        unionid = u'olah40uNjJSuelrQzfaiYFypt55k'
        #client.sendPacket(packet.pack("HsSB",Const.CMD_L2C_WX_LOGIN,Utils.u2pstr(nickname),Utils.u2pstr(headimgurl),sex))
        GameDb.instance.loginWeChat(pid,nickname,headimgurl,sex,openid,unionid)

__cmdTable = {
        Const.CMD_C2L_LOGIN                : cmdLogin,
        Const.CMD_C2L_REGISTER             : cmdRegister,
        Const.CMD_C2L_WX_LOGIN             : cmdWeChatLogin,
        Const.CMD_C2L_AUTO_REGISTER        : cmdAutoRegister
    }

def parse(client,cmd,pkt):
    logging.debug(u"ClientParse cmd = %d",cmd)
    func = __cmdTable.get(cmd)
    if not func:
        logging.error(u"cmd = %d not found",cmd);
    func(client,pkt)

if __name__ == "__main__":
    pass